<%--
    Document   : index
    Created on : 19 juin 2010, 23:51:05
    Author     : marya
--%>

<%@page contentType="text/xml" pageEncoding="UTF-8"%>
<%@page import="org.restlet.resource.ClientResource" %>
<%@page import="org.restlet.representation.Representation" %>
<%@page import="org.restlet.ext.xml.DomRepresentation" %>
<%@page import="org.restlet.Response" %>
<%@page import="org.restlet.Request" %>
<%@page import="org.restlet.data.Method" %>
<%@page import="org.restlet.data.*" %>
<%@page import="org.w3c.dom.Element" %>
<%@page import="org.w3c.dom.Node" %>
<%@page import="org.w3c.dom.NodeList" %>
<%@page import="java.util.List" %>
<%@page import="java.util.Map" %>
<%@page import="xmlws.helpers.WebService" %>
<%
            int idUser = Integer.parseInt(request.getSession().getAttribute("idUser").toString());


            ClientResource client = new ClientResource(WebService.federateur + "/users/" + idUser + "/groupes");

            DomRepresentation representation = null;


            if (request.getMethod().equals("GET")) {
                
                representation = new DomRepresentation(client.get());

                NodeList elements = representation.getDocument().getElementsByTagName("groupe");
                if (elements.getLength() > 0) {
                    for (int i = 0; i < elements.getLength(); i++) {
                        Node node = elements.item(i);
                        String[] tab = node.getAttributes().
                                getNamedItem("url").getTextContent().split("/");

                        String id = tab[tab.length - 2];
                        out.println("<div class='groupe' onclick=\"get_contacts(" + id + ")\" id =\"groupe" + id + "\">");
                        out.println(node.getFirstChild().getTextContent());
                        out.println("</div><br/>");
                    }
                }



            } else if (request.getMethod().equals("POST")) {

                Form form = new Form();


                form.add("nom", request.getParameter("nom"));



                representation = new DomRepresentation(client.post(form.getWebRepresentation()));


                if (client.getResponse().getStatus().equals(Status.SUCCESS_CREATED)) {
                    response.setStatus(200);
                    //Si le status est OK, on affiche le nouveau contact en faisant un get
                    //Sur l'id de contact contenu dans la representation de reponse

                    out.println("<span class='result'>Groupe ajouté</span>");


                } else {


                    response.setStatus(client.getResponse().getStatus().getCode());
                    out.println(client.getResponseEntity().getText());

                }


            }







%>

